package cn.qayy.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import cn.qayy.constants.CollectionConstant;
import cn.qayy.constants.Constant;
import cn.qayy.dto.Msg;
import cn.qayy.dto.PlanLogDTO;
import cn.qayy.dto.PolicyLogDTO;
import cn.qayy.po.PlanLog;
import cn.qayy.service.IPlanLogService;
import cn.qayy.utils.DownLoadUtil;
import cn.qayy.utils.ExcelUtils;
import cn.qayy.utils.JsonUtil;
import cn.qayy.utils.StringUtil;

/**
 * @author wangyu 日志记录管理
 */
@SuppressWarnings("all")
@Controller
@RequestMapping(value = "/planLog/") 
public class PlanLogController {
	// 注入service     
	@Resource(name = "planLogServiceImpl")
	private IPlanLogService planLogService = null;
	
	@ResponseBody
	@RequestMapping(value = "selectForList")
	public Object selectlogList(PlanLogDTO planlog) {
		System.out.println(planlog.toString());
		String resultList = "";
		Msg msg = null;
		try {
			PageInfo<PlanLog> pageInfo = planLogService.selectPlanLogBy(planlog);
			msg = new Msg(Constant.SUCESSFUL_CODE, pageInfo);
			return msg;
		} catch (Exception e) {
			// 异常后的前台提示消息
			e.printStackTrace();
			msg = new Msg(Constant.FAIL_CODE, Constant.FAIL_MSG+e.getMessage());
		}
		return msg;
	}
	
	/***
	 * 页面下载已筛选的记录
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "download")
	public void download(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<PlanLogDTO> loadDataList = DownLoadUtil.getInstance().getTargetDownLoadDataList(PlanLogDTO.class);
			String downloadPath = ExcelUtils.getInstance().dataToExcelDxls("流向日志表",StringUtil.listToStrArray(CollectionConstant.planLog_mb),loadDataList);
			String url = request.getContextPath()+"/servlet/FileDownServlet?url="+downloadPath;
			response.sendRedirect(url);
		} catch (Exception e) {
			e.printStackTrace();
		}  
	}

	/***
	 * 展示UI页面
	 * @return
	 */
	@RequestMapping(value="ui")
	public ModelAndView ui() {
		ModelAndView model = new ModelAndView();
		model.addObject("packs", CollectionConstant.packs);
		model.addObject("products", CollectionConstant.products);
		
		//获取现金流项目集合
		model.setViewName("plan_log");
		return model;
	}
	
}
